package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.MerchantType;
import com.turing.post.organization.util.MerchantTypeDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author zxp
 * @Jan 3, 2013
 */
@Stateless(mappedName = "organization.impl.MerchantTypeDaoImpl")
@Remote
@Local
public class MerchantTypeDaoImpl implements MerchantTypeDao {

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteMerchantTypeByMerchantTypeId(BigInteger merchantTypeId)
			throws PCPSApplicationException {
		String sql = "delete from MerchantType where MerchantTypeId="
				+ merchantTypeId;
		Query query = _em.createNativeQuery(sql);
		try {
			query.executeUpdate();
		} catch (Exception e) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "删除商业类型失败，该商业类型已被引用!");
		}
	}

	@Override
	public MerchantType getMerchantTypeByMerchantTypeId(
			BigInteger merchantTypeId) {
		return _em.find(MerchantType.class, merchantTypeId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantType> getMerchantTypeListByCondition(
			String merchantTypeName, Integer page) {
		String sql = "select t.* from MerchantType t where 1=1";
		// 商业类型名称
		if (!PCPSUtil.isEmpty(merchantTypeName)) {
			sql = sql + " and t.MerchantTypeName like'%" + merchantTypeName
					+ "%'";
		}
		sql = sql + " order by MerchantTypeId";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MerchantType.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantTypeTotalByCondition(String merchantTypeName) {
		String sql = "select nvl(count(*),0) from MerchantType t where 1=1";
		// 商业类型名称
		if (!PCPSUtil.isEmpty(merchantTypeName)) {
			sql = sql + " and t.MerchantTypeName like'%" + merchantTypeName
					+ "%'";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertMerchantType(MerchantType merchantType)
			throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(merchantType.getMerchantTypeName())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "商业类型新增失败，名称为空!");
		}
		merchantType.setMerchantTypeId(commonDaoImpl.getUniqueIntegerKey());
		GeneralDao.insert(_em, merchantType);
	}

	@Override
	public void updateMerchantType(MerchantType merchantType)
			throws PCPSApplicationException {
		MerchantType mt = getMerchantTypeByMerchantTypeId(merchantType
				.getMerchantTypeId());
		mt.setMerchantTypeName(merchantType.getMerchantTypeName());
		mt.setRemark(merchantType.getRemark());
		GeneralDao.insert(_em, mt);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantType> getMerchantTypeList() {
		String sql = "select t.* from MerchantType t order by MerchantTypeId";
		Query query = _em.createNativeQuery(sql, MerchantType.class);
		return query.getResultList();
	}

}
